import warnings
from base64 import b64encode

from compat import urlparse, str, basestring
from _internal_utils import to_native_string

CONTENT_TYPE_FORM_URLENCODED = 'application/x-www-form-urlencoded'
CONTENT_TYPE_MULTI_PART = 'multipart/form-data'


def _basic_auth_str(username, password):
    if not isinstance(username, basestring):
        warnings.warn('username should be a string')
        username = str(username)

    authstr = 'Basic ' + to_native_string(
        b64encode(b':'.join((username, password))).strip()
    )
    return authstr
class AuthBase(object):
    def __call__(self, r):
        raise NotImplementedError('auth hooks must be callable')

class HTTPBasicAuth(AuthBase):
    def __init__(self, username, password):
        self.username = username
        self.password = password

    def __eq__(self, other):
        return all([
            self.username == getattr(other, 'username', None),
            self.password == getattr(other, 'password', None)
        ])

    def __ne__(self, other):
        return not self == other

    def __call__(self, r):
        r.headers['Authorization'] = _basic_auth_str(self.username, self.password)
        return r

class HTTPProxyAuth(HTTPBasicAuth):
    def __call__(self, r):
        r.headers['Proxy-Authorization'] = _basic_auth_str(self.username, self.password)
        return r

